<?php
/**
 * （Logic）解析每日库存报告
 */
namespace App\Logic\Amazon\Parse;

use App\Models\Amazon\FBAMYIInventory;
use App\Util\RExcel;
class ParseMYIInventoryReport
{
    /**
     * 解析FBAMYIInventory
     * @param $file
     * @param $account
     * @param $countryId
     * @return int
     */
    public static function exeParse($file,$account,$countryId){
        if(!$countryId)return 3;
        FBAMYIInventory::query()->where('account_id',$account->id)->where('country_id',$countryId)->delete();
        $lines = RExcel::importFile($file);
        $i = 0;
        $count = 0;
        $head = [];
        foreach($lines as $_str){
            $str = ParseReport::getReplaceStr($_str);
            if($i == 0){
                $head = ParseReport::getTableHeadKey($str,self::$_keys);
                if(empty($head))return 2;
            }else{
                $flag = self::_addFbaMYIInventory($account->id,$countryId,$head,$str);
                $flag && $count++;
            }
            $i++;
        }
        return $i==($count+1) ? 2 : 3;
    }

    private static $_keys = [
        'sku',
        'fnsku',
        'asin',
        'afn-fulfillable-quantity',
        'afn-fulfillable-quantity-local',
        'afn-fulfillable-quantity-remote',
        'afn-reserved-quantity',
        'afn-researching-quantity',
        'afn-inbound-working-quantity',
        'afn-inbound-shipped-quantity',
        'afn-inbound-receiving-quantity'
    ];

    private static $_keys1 = [
        'sku',
        'fnsku',
        'asin',
        'afn-fulfillable-quantity',
        'afn-reserved-quantity',
        'afn-researching-quantity',
        'afn-inbound-working-quantity',
        'afn-inbound-shipped-quantity',
        'afn-inbound-receiving-quantity'
    ];

    private static $_keys2 = [
        'sku',
        'fnsku',
        'asin',
        'afn-fulfillable-quantity-local',
        'afn-fulfillable-quantity-remote',
        'afn-reserved-quantity',
        'afn-researching-quantity',
        'afn-inbound-working-quantity',
        'afn-inbound-shipped-quantity',
        'afn-inbound-receiving-quantity'
    ];

    /**
     * 添加FBAMYIInventory
     * @param $accountId
     * @param $countryId
     * @param $head
     * @param $str
     * @return bool
     */
    private static function _addFbaMYIInventory($accountId,$countryId,$head,$str){
        if(empty($head))return true;
        if(!$str)return true;
        $temp = explode("\t",$str);
        if(array_key_exists('afn-fulfillable-quantity',$head)){
            $keys = self::$_keys1;
        }else{
            $keys = self::$_keys2;
        }
        $flag = false;
        foreach($keys as $key){
            if(!isset($temp[$head[$key]])){
                $flag = true;
                break;
            }
        }
        if($flag)return true;
        if(!$temp[$head['sku']])return true;
        $count = FBAMYIInventory::query()
            ->where('country_id',$countryId)
            ->where('sku',$temp[$head['sku']])
            ->count();
        if(array_key_exists('afn-fulfillable-quantity',$head)){
            $afnFulfillableQuantity = $temp[$head['afn-fulfillable-quantity']];
        }else{
            $afnFulfillableQuantity = $temp[$head['afn-fulfillable-quantity-local']] + $temp[$head['afn-fulfillable-quantity-remote']];
        }
        if($count > 0){
            $flag = FBAMYIInventory::query()
                ->where('country_id',$countryId)
                ->where('sku',$temp[$head['sku']])
                ->update(
                    [
                        'account_id' => $accountId,
                        'fnsku' => $temp[$head['fnsku']],
                        'asin' => $temp[$head['asin']],
                        'afn_fulfillable_quantity' => $afnFulfillableQuantity,
                        'afn_reserved_quantity' => $temp[$head['afn-reserved-quantity']],
                        'afn_researching_quantity' => $temp[$head['afn-researching-quantity']],
                        'afn_inbound_working_quantity' => $temp[$head['afn-inbound-working-quantity']],
                        'afn_inbound_shipped_quantity' => $temp[$head['afn-inbound-shipped-quantity']],
                        'afn_inbound_receiving_quantity' => $temp[$head['afn-inbound-receiving-quantity']],
                    ]
                );
            return !$flag ? false : true;
        }else{
            $re = FBAMYIInventory::query()->create(
                [
                    'country_id' => $countryId,
                    'sku' =>  $temp[$head['sku']],
                    'account_id' => $accountId,
                    'fnsku' => $temp[$head['fnsku']],
                    'asin' => $temp[$head['asin']],
                    'afn_fulfillable_quantity' => $afnFulfillableQuantity,
                    'afn_reserved_quantity' => $temp[$head['afn-reserved-quantity']],
                    'afn_researching_quantity' => $temp[$head['afn-researching-quantity']],
                    'afn_inbound_working_quantity' => $temp[$head['afn-inbound-working-quantity']],
                    'afn_inbound_shipped_quantity' => $temp[$head['afn-inbound-shipped-quantity']],
                    'afn_inbound_receiving_quantity' => $temp[$head['afn-inbound-receiving-quantity']],
                ]
            );
            return !$re ? false : true;
        }
    }
}
